﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.PDA;
using DaInterFace.PDA.P02;

namespace P02
{
    /// <summary>
    /// 执行采样操作
    /// </summary>
    public class A05 : IPDAChild
    {
        public void Code(IPDA handler, CodeResult CR, PDACodeUser user, String parameter)
        {
            P02A05P P = (P02A05P)handler.SerialApi.Deserialize(typeof(P02A05P), parameter, CR.SerialType);
            P02A05R R = new P02A05R();
            try
            {
                SqlParameter[] sps = new SqlParameter[7];
                sps[0] = new SqlParameter("@BillID", SqlDbType.NVarChar, 50);
                sps[0].Value = P.BillID;
                sps[1] = new SqlParameter("@SampID", SqlDbType.NVarChar, 50);
                sps[1].Value = P.SampID;
                sps[2] = new SqlParameter("@RedQty", SqlDbType.Decimal);
                sps[2].Value = P.RedQty;
                sps[3] = new SqlParameter("@RedStyle", SqlDbType.Int);
                sps[3].Value = P.RedStyle;
                sps[4] = new SqlParameter("@RedReason", SqlDbType.NVarChar, 50);
                sps[4].Value = P.RedReason;
                sps[5] = new SqlParameter("@MType", SqlDbType.NVarChar, 3);
                sps[5].Value = P.IsForbid ? "2" : "0";
                sps[6] = new SqlParameter("@User", SqlDbType.NVarChar, 50);
                sps[6].Value = user.UserName;

                Recordset rs = handler.Data.ExeProcedure(P.BaseNo, "EXEC KEW_SAMP_BING @BillID,@SampID,@RedQty,@RedStyle,@RedReason,@MType,@User", sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows[0][0].ToString() == "BING")
                    {
                        R.SampId = rs.Data.Tables[0].Rows[0][1].ToString();
                        R.CarsId = rs.Data.Tables[0].Rows[0][2].ToString();

                        CR.Result = EnumResult.Normal;
                        CR.Message = String.Format("操作员{0}对账套{1}中的取样单进行了绑定{2}：{3}", user.UserName, P.BaseNo, P.BillID, P.SampID);
                        
                        CR.Content = handler.SerialApi.Serializer(typeof(P02A05R), R, CR.SerialType);
                    }
                    else
                    {
                        CR.Result = EnumResult.Error;
                        CR.Message = rs.Data.Tables[0].Rows[0][1].ToString();
                    }
                }
                else
                {
                    CR.Result = EnumResult.Error;
                    CR.Message = "数据库错误";
                }
            }
            catch (Exception ex)
            {
                CR.Result = EnumResult.Error;
                CR.Message = ex.Message;
                handler.Message(MessageType.Error, String.Format("发生错误:{0}", ex.Message));
            }
        }
    }


}
